package com.zyh.controller;

import com.zyh.entity.Student;
import com.zyh.entity.Users;
import com.zyh.service.StudentService;
import com.zyh.service.UserService;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Locale;
import java.util.Objects;

@Controller
public class HomeController {

    @Resource
    private StudentService studentService;

    @Resource
    private UserService userService;
    @RequestMapping("/save")
    public String doSave(@ModelAttribute Student student, HttpSession session){

        if ("true".equals(session.getAttribute("pass"))) {
            student.setName(student.getName().toUpperCase(Locale.ROOT));
            studentService.save(student);
            session.setAttribute("student", student);
        }
        return "studentView/saveSuccess";
    }

    @RequestMapping(value = "/IcheckStudent")
    public String IcheckStudent(Model model, @RequestParam("sno")String sno,Authentication authentication){
        Users users=userService.findByUserName(authentication.getName());
        Student student=studentService.findStudentBySno(Integer.parseInt(sno));
        if (student!=null){
            model.addAttribute("users",users);
            model.addAttribute("student",student);
            return "InspectorView/inspectorDo";
        }else{
            return "error";
        }
    }
    @RequestMapping(value = "/WcheckStudent")
    public String WcheckStudent(Model model, @RequestParam("sno")String sno, Authentication authentication) {
        Users users=userService.findByUserName(authentication.getName());
        Student student=studentService.findStudentBySno(Integer.parseInt(sno));
        if (student!=null){
            if(Objects.equals(student.getHealthy(), "Y") || Objects.equals(student.getHealthy(), "y")){
                model.addAttribute("users",users);
                model.addAttribute("student",student);
                return "workerView/workerDo";
            }else{
                return "workerView/werror";
            }
        }else{
            return "error";
        }
    }
@RequestMapping("/hTable")
    public String hTable(Model model,Authentication authentication){
        return "studentView/healthTable";
    }

}
